<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" rel="stylesheet">
    <title>Python 成绩查询</title>
    <style>
        body {
            background: #f5f5f5;
        }
        .container {
            padding-top: 45px;
        }
        #score {
            font-size: 22px;
            text-align: center;
            margin-bottom: 20px;
        }
        #table-a, #table-b {
            display: none;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-dark bg-dark">
        <span class="navbar-brand">Python 成绩查询</span>
    </nav>
    <div class="container">
        <div class="row">
            <div class="col-md-2"></div>
            <div class="col-md-8">
                <div class="card">
                    <div class="card-body">
                        <div class="card-title">期中考试成绩查询</div>
                        <hr />
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="stuno-id">学&nbsp;&nbsp;号: </span>
                            </div>
                            <input id="id" type="text" class="form-control" aria-describedby="stuno-id">
                        </div>
                        <br />
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="stuno-id">姓&nbsp;&nbsp;名: </span>
                            </div>
                            <input id="name" type="text" class="form-control" aria-describedby="stuno-id">
                        </div>
                        <br />
                        <table class="table" id="table-a">
                            <tr>
                                <th>总分</th>
                                <th>0</th>
                                <th>1-a</th>
                                <th>1-b</th>
                                <th>1-c</th>
                                <th>1-d</th>
                                <th>2-a</th>
                                <th>2-b</th>
                            </tr>
                            <tr id="tr-a"></tr>
                        </table>
                        <table class="table" id="table-b">
                            <tr>
                                <th>总分</th>
                                <th>0</th>
                                <th>1-a</th>
                                <th>1-b</th>
                                <th>1-c</th>
                                <th>2-a</th>
                                <th>2-b</th>
                            </tr>
                            <tr id="tr-b"></tr>
                        </table>
                        <div style="text-align: center;">
                            <button id="btn" class="btn btn-primary">查&nbsp;&nbsp;询</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script id="sc" src="score.js"></script>
<script>
	d = new Date();
	document.querySelector('#sc').setAttribute('src','score.js?='+d.getTime());

    idEl = document.querySelector('#id');
    nameEl = document.querySelector('#name');
    btnEl = document.querySelector('#btn');
    tableA = document.querySelector('#table-a');
    tableB = document.querySelector('#table-b');
    trA = document.querySelector('#tr-a');
    trB = document.querySelector('#tr-b');

    btnEl.onclick = function() {
        var retA = {};
        sa.forEach(function(item) {
            if (item['name'] === nameEl.value &&
                item['id'] === idEl.value) {
                retA = item;
            }
        });
        var retB = {};
        sb.forEach(function(item) {
            if (item['name'] === nameEl.value &&
                item['id'] === idEl.value) {
                retB = item;
            }
        });

        if (!retA['scores'] && !retB['scores']) {
            alert('未查询到结果，请检查信息是否输入正确！');
        } else {
            btnEl.style.display = 'none';
            if (retA['scores']) {
                tableA.style.display = 'table';

                retA['scores'].forEach(function(item) {
                    var td = document.createElement('td')
                    td.innerText = item;
                    trA.appendChild(td);
                });
            } else if (retB['scores']) {
                tableB.style.display = 'table';

                retB['scores'].forEach(function(item) {
                    var td = document.createElement('td')
                    td.innerText = item;
                    trB.appendChild(td);
                });
            }
        }
    };
</script>
</html>
